#include <jni.h>
#include <string>
#include "log/androilog.h"
#include "MediaInfo.h"
#include "ExtrAudioModule.h"
#include "ExtrVideoModule.h"

using namespace std;


extern "C"

/**
 * java 调用jni  包名_类名_方法名(env环境，调用者对象实例)
 * @param env
 * @return
 */

JNIEXPORT jstring JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    string hello = "java 调用 jni 获取 c to jni ->return 给Java层";
    LOGD("java call jni");
    return env->NewStringUTF(hello.c_str());
}


/**
 * java 调用jni 静态方法 包名_类名_方法名(env环境，调用者class本身)
 * @param env
 * @return
 */
extern "C"
JNIEXPORT jstring JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_staticFromJNI(JNIEnv *env, jclass type) {
    string returnValue = "java 调用 jni静态方法 获取 c to jni ->return 给Java层";
    return env->NewStringUTF(returnValue.c_str());
}extern "C"

/**
 * 获取java层传递过来的数据
 * @param env
 * @param instance
 * @param vaule_
 */
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_postValue2JNI(JNIEnv *env, jobject instance,
                                                             jstring vaule_) {
    //将jni转换为c char *
    const char *vaule = env->GetStringUTFChars(vaule_, 0);
    LOGD(vaule);
    //开辟了jvm空间，释放空间
    env->ReleaseStringUTFChars(vaule_, vaule);

}

/**
 *  JAVA 传递对象给jni获取对象中的数据转换成 jni对象 -》转换成c++对象
 * https://blog.csdn.net/niuba123456/article/details/80993796
 */
extern "C"
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_postValueOBJ2JNI(JNIEnv *env, jobject instance,
                                                                jobject postObject) {

    //1.转换为jclass 或使用findclass FindClass("com/niubashaoye/simple/jni/StuInfo")
    jclass postData = env->GetObjectClass(postObject);

    //2.获取jvm属性smali 对象id
    jfieldID nameId = env->GetFieldID(postData, "name", "Ljava/lang/String;");

    //3.转换成jni string
    jstring nameStr = (jstring) (env->GetObjectField(postObject, nameId));

    if (nameStr == NULL) {
        return;
    }
    //4.转换成c++
    const char *name = env->GetStringUTFChars(nameStr, 0);

    LOGD(name);
    //释放name
    env->ReleaseStringUTFChars(nameStr, name);

    //释放postObject对象
    //  env->DeleteLocalRef(postObject);


}
/**
 *
 * jni调用java方法
 */
extern "C"
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_accessMethod(JNIEnv *env, jobject instance) {
    jclass _classMain = env->GetObjectClass(instance);
    jmethodID mid = env->GetMethodID(_classMain, "javaMethod", "(I)I");
    jint _random = env->CallIntMethod(instance, mid, 100);
    LOGD("%d", _random);


}extern "C"
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_accessStaticMethod(JNIEnv *env, jobject instance) {

    jclass _clz = env->GetObjectClass(instance);

    jmethodID mid = env->GetStaticMethodID(_clz, "javaStaticMethod", "()Ljava/lang/String;");
    jstring _uuid = (jstring) env->CallStaticObjectMethod(_clz, mid);

    const char *uuid = env->GetStringUTFChars(_uuid, 0);
    LOGD(uuid);

    env->ReleaseStringUTFChars(_uuid, uuid);

}extern "C"
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_accessFiled(JNIEnv *env, jobject instance) {

    /*jclass _clz = env->GetObjectClass(instance);
    jfieldID _fid = env->GetFieldID(_clz, "accessValue", "Ljava/lang/String;");
    jstring _value = (jstring) env->GetObjectField(instance, _fid);
    const char *temp = "set vaule from jni";
    const char *value = env->GetStringUTFChars(_value, 0);
    LOGD(value);

    jstring _setVaule = env->NewStringUTF(temp);
    env->SetObjectField(instance, _fid, _setVaule);
    env->ReleaseStringUTFChars(_value, value);
  //  env->ReleaseStringUTFChars(_setVaule,temp);
    jthrowable  _ex = env->ExceptionOccurred();

    if(_ex !=NULL){
        env->ExceptionClear();
    }*/



}

/*
extern "C"
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_initFFmpeg(JNIEnv *env, jobject instance,
                                                          jstring path_) {
    const char *path = env->GetStringUTFChars(path_, 0);

   */
/* LOGD(path);

    MediaInfoMain mediaInfoMain(path);

    mediaInfoMain.getMediaInfo();*//*


    env->ReleaseStringUTFChars(path_, path);
}*/
extern "C"
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_test(JNIEnv *env, jobject instance) {


}extern "C"
JNIEXPORT void JNICALL
Java_cn_cryengine3_com_kvmaker_MainNativBridge_makeffmpegInfo(JNIEnv *env, jobject instance,
                                                              jstring path_, jstring target_) {
    const char *path = env->GetStringUTFChars(path_, 0);
    const char *targe = env->GetStringUTFChars(target_, 0);

  /*  MediaInfoMain mediaInfoMain(path);
    mediaInfoMain.getMediaInfo();

    ExtrAudio extrAudio;
    extrAudio._extrAudio(path, targe);*/

    ExtrVideoModule extrVideoModule;
    extrVideoModule._extrVideo(path, targe);


    env->ReleaseStringUTFChars(path_, path);
}